//
//  PostPictureCell.m
//  sensen_ios
//
//  Created by mc on 16/3/25.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostPictureCell.h"
#import "BigImageViewController.h"


#define Margin 13

@implementation PostPictureCell



+(instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString * cellIdentifier = @"PostPictureCell";
    PostPictureCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
    
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self =  [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        
        
    }
    return self;
}
- (void)setupSubViews{
   
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    

    
    for (int i = 0; i < _array.count; i++) {
        //CGSize Size = [UIImage getImageSizeWithURL:_array[0]];
        CGFloat width = (SCREEN_WIDTH - Margin * 4) / 3;
        //CGFloat heigth = width * (Size.height / Size.width);
        CGFloat x = Margin * (i % 3 + 1) + width * (i % 3) ;
        CGFloat y = Margin * (i / 3) + width * (i / 3) + 5;
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, width)];
        //[imageView setImageURLStr:_array[i] placeholder:DEFAULT_IMAGE];
        __strong UIImageView *s_view = imageView;
        [imageView setImageWithURL:_array[i] placeholderImage:DEFAULT_CAROUSEL_IMAGE completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            // 设置正确的图片尺寸
            s_view.image = [image getCenterImageWithRatio:1]; // 根据宽高比1：1获得图片
        }];

        
        [self.contentView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        imageView.tag = i + 100;
    }
   
    
   
}
- (void)setPostModel:(PostModel *)postModel{
    
    _postModel = postModel;
    _array = _postModel.imgAry;
    [self setupSubViews];
    
}
- (void)setPostReplyModel:(PostReplyModel *)postReplyModel{
    _postReplyModel = postReplyModel;
    _array = postReplyModel.img;
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    BigImageViewController *vc = [[BigImageViewController alloc] init];
    vc.imageData = _array;
    NSInteger index = tap.view.tag - 100;
    vc.indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.viewController presentViewController:vc animated:YES completion:nil];
    
}

@end
